// pages/scheHis/scheduleList/scheduleList.js
import validate from '../../../utils/validate.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid : '',
    gid:'',
    storeid:'',
    storename:'',
    userList:[],
    date:'',
    scheduleList:[],
    CustomBar:app.globalData.CustomBar,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      storeid:options.storeid,
      storename:options.storename
    })
    this.setData({
      date:this.getNowDate()
    })
    this.getUsers()
  },
  getNowDate:function(){ //获取当前日期字符串返回
    let nowDate =  new Date();
    let m = ((nowDate.getMonth() + 1) + '').padStart(2,'0');
    let d = ((nowDate.getDate()) + '').padStart(2,'0');
    return nowDate.getFullYear() + '-' + m +'-' + d;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.search()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  search:function(){
    let that = this;
    let data = {storeid:this.data.storeid}
    app.api.getRecordlist(data).then(res => {
      if(res.code == 200){
        that.setData({
          scheduleList : res.rows,
          checkeds:[]
        })
      }
    })
  },
  selSchedule:function(){
    let schedule = this.data.scheduleList[e.currentTarget.dataset.index]

  },
  bindTimeChange:function(e){
    this.setData({
      date:e.detail.value
    })
    this.search()
  },

  changeNum:function(e){
    this.setData({
      num: this.data.nums[e.detail.value]
    })
    this.search()
  },
  checkboxChange:function(e){
    this.setData({
      checkeds : e.detail.value
    })
    // let list = JSON.parse(JSON.stringify(this.data.scheduleList));
    // for(let index in list){
    //   list[index].checked = false
    //   for(let val of e.detail.value){
    //     if(index = val){
    //       list[index].checked = true;
    //       break;
    //     }
    //   }
    // }
  },
  selUser:function(e){
    this.setData({
      userid : e.currentTarget.dataset.userid,
      gid : e.currentTarget.dataset.gid
    })
  },
  getUsers:function(){
    app.api.getMyPromlist(this.data.storeid).then(res => {
      console.log(res)
      this.setData({
        userList:res.rows
      })
    })
  },
  changeUser:function(e){
    console.log(e)
    let userid = this.data.userList[e.detail.value].userid;
    this.updateUser(userid)
  }
  ,
  updateUser:function(userid){
    let data = {gid:this.data.gid,storeid:this.data.storeid,olduserid:this.data.userid,newuserid:userid}
    app.api.updateUser(data).then(res => {
     this.setData({
       userid : ''
     })
    })
    this.search();
  }
})